<?php

namespace App\Http\Controllers\Admin;

use App\Api\Serializer\CustomSerializer;
use App\Http\Controllers\BaseController;
use App\Http\Requests\Auth\RegisterRequest;
use App\Transformers\UserTransformer;
use App\Models\User;
use Illuminate\Http\Request;

class UserController extends BaseController
{
    //用户列表
    public function index(Request $request)
    {
        $name  = $request->input('name');
        $email = $request->input('email');
        $user  = User::when($name,function ($query) use($name){
           $query->where('name','like',"%$name%");
        })
            ->when($email,function ($query) use($email){
                $query->where('email','=',$email);
            })
            ->get();

        return $this->response->item($user,new UserTransformer(),function ($resource, $fractal) {
            $fractal->setSerializer(new CustomSerializer());
        });
    }


//详细信息
    public function show(User $user)
    {

         return $this->response->item($user,new UserTransformer(),function ($resource, $fractal) {
             $fractal->setSerializer(new CustomSerializer());
         });
    }
    //添加用户
    public function store(RegisterRequest $request){
        User::create($request->all());
        return $this->response->created();
    }



    ///禁用与启动
    public function lock(User $user){
        $user->is_locked = $user->is_locked==0?1:0;
        $user->save();
        return $this->response->noContent();
    }

}
